import Mock from "mockjs";

Mock.mock(RegExp('/api/save/' + ".*"),'post',(param)=>{
  console.log(param.url,JSON.parse(param.body))
  return Mock.mock({
    success: true,
    msg: "操作成功！",
    code: 0,
    result: Mock.Random.guid(),
    timestamp: "@now",
  });
})

Mock.mock(RegExp('/api/delete/' + ".*"),'post',(param)=>{
  console.log(param.url,JSON.parse(param.body))
  return Mock.mock({
    success: true,
    msg: "操作成功！",
    code: 0,
    "result|1-100": 'number',
    timestamp: "@now",
  });
})

/**
 * 通用查询方法，默认返回所有类型的数据，可以在参数中传递"_mock"属性来生成自定义的模拟数据
 */
Mock.mock(RegExp('/api/.*/list/' + ".*"),'post',(param)=>{
  console.log(param)
  const body = JSON.parse(param.body)
  if (body.hasOwnProperty('_mock')){
    const data = Mock.mock({
      success: true,
      msg: "操作成功！",
      code: 0,
      result: {
        "rows|20":[body._mock],
        total:"@integer(100,1000)"
      },
      timestamp: "@now",
    });
    console.log(param.url,JSON.parse(param.body),data)
    return data
  }else{
    
    const data = Mock.mock({
      success: true,
      msg: "操作成功！",
      code: 0,
      result: {
        "rows|20":[{  // 生成20条数据
          "id": "@guid",   // guid
          "arr":Mock.Random.pick(["1", "2", "3", "4", "5"],1,5),  // 数组内随机一个值
          "email":"@email",
          "ip":"@ip",
          "url":"@url",
          "imgUrl":Mock.Random.image('200x100', Mock.mock("@color"), '测试图片'),
          "base64":Mock.Random.dataImage('200x200', '测试图片'),
          "addre":"@county(true)",
          "isYes":"@boolean",
          "int":"@integer(0,100)",
          "float":"@float(1,100,3)",
          "str":"@string(3,20)",
          "name":"@cname",
          "title":"@ctitle",
          "content":"@csentence(3, 500)",
          "datetime": new Date(),
          "role|1": ["测试角色1", "测试角色2", "测试角色3", "测试角色4", "测试角色5"],
          "updateTime": '@now',
        }],
        "total|100-1000":100
      },
      timestamp: "@now",
    });
    console.log(param.url,JSON.parse(param.body),data)
    return data
  }
})



Mock.mock('/api/upload/img','post',params=>{
  return {
    success: true,
    msg: "操作成功！",
    code: 0,
    result: {
      url:Mock.Random.image('200x200',Mock.Random.color(),'测试图片'),
      base64:Mock.Random.dataImage('200x200','测试图片')
    },
    timestamp: "@now",
  };
})

Mock.mock('/api/upload/file','post',params=>{
  return {
    success: true,
    msg: "操作成功！",
    code: 0,
    result: {
      url:Mock.Random.image('200x200',Mock.Random.color(),'测试图片'),
      base64:Mock.Random.dataImage('200x200','测试图片')
    },
    timestamp: "@now",
  };
})
